package cn.swpu.blog.controller;

import cn.swpu.blog.entity.Tag;
import cn.swpu.blog.service.ITagService;
import cn.swpu.blog.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 冰翔
 * @Date Created in 4:53 下午 2020/12/11
 */
@Api(tags = "标签Tag")
@RequestMapping("/tag")
@RestController
public class TagController {

    @Autowired
    private ITagService tagService;

    @PreAuthorize("permitAll()")
    @ApiOperation("查询单个标签")
    @GetMapping("/{tagId}")
    public ResponseVo<Map<String, String>> queryTag(@PathVariable Integer tagId) {
        return tagService.queryTag(tagId);
    }

    @PreAuthorize("permitAll()")
    @ApiOperation("根据List查询标签")
    @PostMapping("/list")
    public ResponseVo<List<Tag>> queryTagList(@RequestBody List<Integer> list) {
        return tagService.queryTagByList(list);
    }

    @PreAuthorize("permitAll()")
    @ApiOperation("查询所有标签")
    @GetMapping("/all")
    public ResponseVo<List<Tag>> queryAllTag() {
        return tagService.queryAll();
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("创建标签")
    @PostMapping("/insert")
    public ResponseVo<Map<String, Integer>> insertTag(@RequestParam String tag) {
        return tagService.createTag(tag);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("根据id删除标签")
    @DeleteMapping("/delTag/{tagId}")
    public ResponseVo<Tag> deleteTag(@PathVariable Integer tagId){
        return tagService.deleteTag(tagId);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("根据ID更改标签")
    @PutMapping("/updateTag")
    public ResponseVo<Tag> updateTag(@RequestBody Tag tag){
        return tagService.updateTag(tag);
    }
}
